package com.example.myshixun;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MyShixun2 extends AppCompatActivity {
    private static final String TAG = "";
    private EditText etusername, etpassword, etpassagain;
    ImageView ivretu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_shixun2);
        etusername = findViewById(R.id.et_username);
        etpassword = findViewById(R.id.et_password);
        etpassagain = findViewById(R.id.et_pass_again);
        ivretu = findViewById(R.id.iv_retu);
        ivretu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent retu = new Intent();
                retu.setClass(MyShixun2.this,MainActivity.class);
                startActivity(retu);
            }
        });
        Button register = findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etusername.getText().toString()) ||
                        TextUtils.isEmpty(etpassword.getText().toString()) ||
                        TextUtils.isEmpty(etpassagain.getText().toString())) {
                    Toast.makeText(MyShixun2.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    login();
                }
            }
        });
    }

    private void login() {

        if (etpassword.getText().toString().equals(etpassagain.getText().toString())){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    OkHttpClient client = new OkHttpClient();
                    String url = "http://192.168.124.126:8080/18/All/user?telephone="+etusername.getText().toString();
                    FormBody body = new FormBody.Builder().build();
                    Request request = new Request.Builder().url(url).post(body).build();
                    final Call call = client.newCall(request);
                    try {
                        Response response = call.execute();
                        String data = "";
                        data = response.body().string();
                        Log.e(TAG,"run"+data);
                        if (data.length()>4){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyShixun2.this,"该用户已存在！",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }else {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    OkHttpClient addClient = new OkHttpClient();
                                    FormBody body = new FormBody.Builder()
                                            .add("telephone",etusername.getText().toString())
                                            .add("password",etpassword.getText().toString())
                                            .build();
                                    String url = "http://192.168.124.126:8080/18/All/add";
                                    Request request1 = new Request.Builder().url(url).post(body).build();
                                    Call addCall = addClient.newCall(request1);
                                    try {
                                        Response addResponse = addCall.execute();
                                        final String data1 = addResponse.body().string();
                                        if (data1.equals("\""+"成功"+"\"")){
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Toast.makeText(MyShixun2.this,"注册成功！",Toast.LENGTH_SHORT).show();
                                                    Intent intent = new Intent(MyShixun2.this,MainActivity.class);
                                                    startActivity(intent);
                                                }
                                            });
                                        }else {
                                            Log.e(TAG,"run"+data1);
                                            Toast.makeText(MyShixun2.this,"注册失败",Toast.LENGTH_SHORT).show();
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }

                                }
                            }).start();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }else {
            Toast.makeText(MyShixun2.this,"两次输入密码不一致",Toast.LENGTH_SHORT).show();
            Log.e(TAG,"toRegist"+etpassword+"...."+etpassagain);
        }
    }
//    public static class MD5Utils{
//        public static String md5(String text){
//            MessageDigest digest = null;
//            try {
//                digest = MessageDigest.getInstance("md5");
//                byte[] result = digest.digest(text.getBytes());
//                StringBuffer sb = new StringBuffer();
//                for (byte b : result){
//                    int number = b & 0xff;
//                    String hex = Integer.toHexString(number);
//                    if (hex.length()==1){
//                        sb.append("0"+hex);
//                    }else {
//                        sb.append(hex);
//                    }
//                }
//                return sb.toString();
//            } catch (NoSuchAlgorithmException e) {
//                e.printStackTrace();
//                return "";
//            }
//        }
//    }

}
//                Intent li = new Intent();
//                li.setClass(MyShixun2.this,MyShixun3.class);
//               startActivity(li);
       